package com.ruoyi.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;

import com.ruoyi.common.core.exception.pay.LimitException;
import com.ruoyi.pay.config.AliPayConfig;
import com.ruoyi.pay.config.PayUrlConfig;

import com.ruoyi.pay.domain.Order;
import com.ruoyi.pay.domain.dto.OrderDTO;
import com.ruoyi.pay.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author: Larry
 * @Date: 2024 /03 /27 / 13:37
 * @Description:
 */
@Slf4j
@Service
public class AliPayService {
    @Resource
    IOrderService orderService;
//    @Resource
//    private KafkaTemplate<String, String> kafkaTemplate;
    @Resource
    private PayUrlConfig payUrlConfig;
    @Value("${kafka.topic.group}")
    private String topicName;
    public String genOrder(OrderDTO orderDTO)  {
        try {
            System.out.println(orderDTO);
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
            long tradeNo = System.currentTimeMillis();
            model.setOutTradeNo(String.valueOf(tradeNo));
            model.setTotalAmount(orderDTO.getMoney().toString());
            model.setSubject("捐助"+orderDTO.getMoney()+"元");
            // 支付的请求体参数
            JSONObject json = new JSONObject();
            json.put("templateId", UUID.randomUUID());
            json.put("fee", "0.1");
            String params = json.toString();
            model.setBody(params);
            model.setTimeoutExpress("30m");
            // 设置商户门店编号
            // 2: 获取阿里请求对象
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            // 3：设置请求参数的集合，最大长度不限
            request.setBizModel(model);
            // 设置异步回调地址
            request.setNotifyUrl(payUrlConfig.getCallbackUrl());
            AlipayTradePrecreateResponse alipayTradePrecreateResponse = null;
            try {
                alipayTradePrecreateResponse = AliPayConfig.getInstance().execute(request);
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            assert alipayTradePrecreateResponse != null;
            JSONObject jsonObject = JSON.parseObject(alipayTradePrecreateResponse.getBody());
            log.info("# json obj:{}",jsonObject);
            Order order = BeanUtil.copyProperties(orderDTO,Order.class);
            orderService.insertOrder(order);
            //jsonObj中存在支付图片的URL信息，直接返回即可。创建完订单需要把订单相关信息保存起来，后续自己去调用
            //绘制二维码的任务交给，前端自己去处理
            return alipayTradePrecreateResponse.getQrCode();
        } catch (LimitException e) {
            throw new LimitException("不要频繁操作");
        }
    }
}
